package com.ruoyi.web.controller.platform;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.db.DSqlKit;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 宫颈癌病史Controller
 *
 * @author ruoyi
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/platform/gja")
public class GjgbsController extends BaseController
{


    /**
     * 查询宫颈癌病史列表
     */

    @GetMapping("/bslist")
    public TableDataInfo bslist(HttpServletRequest request)
    {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        System.out.println(loginUser);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageIndex = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        System.out.println(request);
        String select="select g.SAVE_FILE,g.UPLOAD,g.PE_ID,g.PE_VISIT_ID,g.ID_NO,g.LAST_MENSTRUAL_PERIOD," +
                "g.TREATED," +
                "g.TREATED_YEAR,g.dept_result,g.result,f.`EXAMINATION` ,\n" +
                "  f.`CONDYLOMA_ACUMINATUM`,\n" +
                "  f.`TRICHOMONAS_VAGINITIS` ,\n" +
                "  f.`VULVOVAGINAL_CANDIDIASIS` ,\n" +
                "  f.`BACTERIAL_VAGINOSIS` ,\n" +
                "  f.`MUCOPURULENT_CERVICITIS` ,\n" +
                "  f.`CERVICAL_POLYP` ,\n" +
                "  f.`FIBROID` ,\n" +
                "  f.`OTHER` ,\n" +
                "  g.`UPLOADRESULT` ,\n" +
                "  g.CHECK_DATE,g.CHECK_PERSON,g.SAVE_DATE ,g.create_time";
        DSqlKit.init(" from gjgbs g " +
                "left join gjgfkjc f on f.PE_ID=g.PE_ID and f.PE_VISIT_ID=g.PE_VISIT_ID where 1=1 ");
        logger.info(request.getParameterNames().toString());
        //上传状态
        if(!ObjectUtils.isEmpty(request.getParameter("templateId")))
        {
            DSqlKit.append(" and (g.UPLOAD = ?) ", request.getParameter("templateId").toString());
        }
        if(!ObjectUtils.isEmpty(request.getParameter("saveFile")))
        {
            DSqlKit.append(" and (g.SAVE_FILE = ?) ", request.getParameter("saveFile").toString());
        }


        if(!ObjectUtils.isEmpty(request.getParameter("createDateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("createDateRange[1]")))
        {

            DSqlKit.append(" and (g.create_time BETWEEN ? and ? ) ", request.getParameter("createDateRange[0]"),request.getParameter("createDateRange[1]"));
        }
        else
        {
           // DSqlKit.append(" and (g.create_time > sysdate()-30 ) ");
         }

        if(!ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("dateRange[1]")))
        {

            DSqlKit.append(" and (g.SAVE_DATE BETWEEN ? and ? ) ", request.getParameter("dateRange[0]"),request.getParameter("dateRange[1]"));
        }

        if(!ObjectUtils.isEmpty(request.getParameter("PE_ID")))
        {
            String val = "%" +request.getParameter("PE_ID").toString()+ "%";
            DSqlKit.append(" and (g.PE_ID like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("PE_VISIT_ID")))
        {
            DSqlKit.append(" and (g.PE_VISIT_ID = ?) ", request.getParameter("PE_VISIT_ID").toString());
        }
        if(!ObjectUtils.isEmpty(request.getParameter("ID_NO")))
        {
            String val = "%" +request.getParameter("ID_NO").toString()+ "%";
            DSqlKit.append(" and (g.ID_NO like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("BH")))
        {
            String val = "%" +request.getParameter("BH").toString()+ "%";
            DSqlKit.append(" and (g.BH like ?) ", val);
        }
        if(!ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
        {
            String val = "%" +request.getParameter("UPLOAD").toString()+ "%";
            DSqlKit.append(" and (g.UPLOAD like ?) ", val);
        }
        if(ObjectUtils.isEmpty(request.getParameter("templateId")) && ObjectUtils.isEmpty(request.getParameter("saveFile")) && ObjectUtils.isEmpty(request.getParameter("createDateRange[0]"))
        && ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && ObjectUtils.isEmpty(request.getParameter("PE_ID")) && ObjectUtils.isEmpty(request.getParameter("ID_NO")) && ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
        {
          DSqlKit.append(" and ((g.SAVE_FILE = '0') or (g.SAVE_DATE>  CURDATE()-1)) ");
        }
        DSqlKit.append(" order by  CHECK_DATE desc ");
        Page<Record> page =  Db.paginate(pageIndex, pageSize,select,DSqlKit.getSql(),DSqlKit.getParamList());
        String[] tmp={"CONDYLOMA_ACUMINATUM","TRICHOMONAS_VAGINITIS","VULVOVAGINAL_CANDIDIASIS",
        "BACTERIAL_VAGINOSIS","MUCOPURULENT_CERVICITIS","CERVICAL_POLYP","FIBROID"};
        for(Record data:page.getList())
        {
            for(String tmpStr:tmp)
            {
                if(data.get(tmpStr)!=null)
                {
                    if(data.getStr(tmpStr).equals("1"))
                    {
                        data.set(tmpStr,true);
                    }
                    else
                    {
                        data.set(tmpStr,false);
                    }
                }
                else
                {
                    data.set(tmpStr,false);
                }
            }
        }
        return getDataTable(page);
    }

    @ResponseBody
    @Log(title = "宫颈癌病史审核保存", businessType = BusinessType.UPDATE)
    @PostMapping("/bssave")
    public AjaxResult bssave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Boolean flag=bssaveData(data,loginUser.getUsername());
        if(flag)
        {
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error();
        }

    }


    @ResponseBody
    @Log(title = "宫颈癌病史审核批量保存", businessType = BusinessType.UPDATE)
    @PostMapping("/bsbatchSave")
    public AjaxResult batchSave(@RequestBody(required = true) Map<String,Object> data)
    {
        System.out.println(data);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String Msg="";
        List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
        for(Map<String,Object> tmp : list)
        {
            if(tmp.get("upload")==null)
            {
                Boolean flag=bssaveData(tmp,loginUser.getUsername());
                if(!flag)
                {
                    Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                }
            }
            else
            {
                if(!tmp.get("upload").toString().equals("1"))
                {
                    Boolean flag=bssaveData(tmp,loginUser.getUsername());
                    if(!flag)
                    {
                        Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                    }
                }
            }
        }
        return AjaxResult.success(Msg);
    }


    public boolean bssaveData(Map<String,Object> data,String userName)
    {
        Record saveGjgbsData=new Record();
        saveGjgbsData.set("PE_ID",data.get("peId"));
        saveGjgbsData.set("PE_VISIT_ID",data.get("peVisitId"));
        saveGjgbsData.set("ID_NO",data.get("idNo"));
        if(data.get("lastMenstrualPeriod")!=null)
        {
            saveGjgbsData.set("LAST_MENSTRUAL_PERIOD",data.get("lastMenstrualPeriod").toString().substring(0,10));
        }
        saveGjgbsData.set("TREATED",data.get("treated"));
        saveGjgbsData.set("TREATED_YEAR",data.get("treatedYear"));
        saveGjgbsData.set("SAVE_DATE",new Date());
        saveGjgbsData.set("SAVE_PERSON",userName);
        saveGjgbsData.set("SAVE_FILE","1");
        saveGjgbsData.set("dept_result",data.get("deptResult"));
        saveGjgbsData.set("result",data.get("result"));
        Record saveGjgfkjcData=new Record();
        saveGjgfkjcData.set("PE_ID",data.get("peId"));
        saveGjgfkjcData.set("PE_VISIT_ID",data.get("peVisitId"));
        saveGjgfkjcData.set("EXAMINATION","0");


        Map<String,String> tmpMap=new HashMap<>();

        tmpMap.put("condylomaAcuminatum","CONDYLOMA_ACUMINATUM");
        tmpMap.put("trichomonasVaginitis","TRICHOMONAS_VAGINITIS");
        tmpMap.put("vulvovaginalCandidiasis","VULVOVAGINAL_CANDIDIASIS");
        tmpMap.put("bacterialVaginosis","BACTERIAL_VAGINOSIS");
        tmpMap.put("mucopurulentCervicitis","MUCOPURULENT_CERVICITIS");
        tmpMap.put("cervicalPolyp","CERVICAL_POLYP");
        tmpMap.put("fibroid","FIBROID");
//        tmpMap.put("other","OTHER");

        for(String key:tmpMap.keySet())
        {
            if(data.get(key)==null)
            {
                saveGjgfkjcData.set(tmpMap.get(key),"0");
            }
            else
            {
                if((Boolean) data.get(key))
                {
                    saveGjgfkjcData.set(tmpMap.get(key),"1");
                    saveGjgfkjcData.set("EXAMINATION","1");
                }
                else
                {
                    saveGjgfkjcData.set(tmpMap.get(key),"0");
                }
            }
        }



        saveGjgfkjcData.set("CHECK_DATE",data.get("checkDate"));
        saveGjgfkjcData.set("CHECK_PERSON",data.get("checkPerson"));
        saveGjgfkjcData.set("CHECK_PERSON",data.get("checkPerson"));
        saveGjgfkjcData.set("SAVE_DATE",new Date());
        saveGjgfkjcData.set("SAVE_PERSON",userName);
        saveGjgfkjcData.set("SAVE_FILE","1");

        Boolean flag=Db.tx(() -> {
            Db.update("gjgbs","PE_ID,PE_VISIT_ID",saveGjgbsData);
            Db.update("gjgfkjc","PE_ID,PE_VISIT_ID",saveGjgfkjcData);
            return true;
        });
        return flag;
    }
//*************************************************hpv**********************************************************************
@GetMapping("/hpvlist")
public TableDataInfo hpvlist(HttpServletRequest request)
{

    PageDomain pageDomain = TableSupport.buildPageRequest();
    Integer pageIndex = pageDomain.getPageNum();
    Integer pageSize = pageDomain.getPageSize();

    String select="select     PE_ID ,  PE_VISIT_ID,  ID_NO ,  P_ID ,  HPV_CHECK,  SIXTEEN,  EIGHTEEN ,  THIRTY_ONE,  THIRTY_THREE,  THIRTY_FIVE,\n" +
            "  FORTY_FIVE,  FIFTY_TWO,  FIFTY_EIGHT,  FURTHER_EXAMINATION,  CERVICAL_CYTOLOGY,  VIA,  COLPOSCOPY,  HPV_TYPING,\n" +
            "  REPORT_DATE,  REPORT_PERSON,  SAVE_FILE,  UPLOAD,  OTHER,  AUTO_VALUE,  PE_DATE,  DOCTOR_NAME,  UPLOADRESULT,dept_result,result,create_time ";
    DSqlKit.init(" from gjghpvjc  where 1=1 ");



    //上传状态
    if(!ObjectUtils.isEmpty(request.getParameter("templateId")))
    {
        DSqlKit.append(" and (UPLOAD = ?) ", request.getParameter("templateId").toString());
    }
    if(!ObjectUtils.isEmpty(request.getParameter("saveFile")))
    {
        DSqlKit.append(" and (SAVE_FILE = ?) ", request.getParameter("saveFile").toString());
    }


    if(!ObjectUtils.isEmpty(request.getParameter("createDateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("createDateRange[1]")))
    {

        DSqlKit.append(" and (create_time BETWEEN ? and ? ) ", request.getParameter("createDateRange[0]"),request.getParameter("createDateRange[1]"));
    }
    if(!ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("dateRange[1]")))
    {

        DSqlKit.append(" and (REPORT_DATE BETWEEN ? and ? ) ", request.getParameter("dateRange[0]"),request.getParameter("dateRange[1]"));
    }

    if(!ObjectUtils.isEmpty(request.getParameter("PE_ID")))
    {
        String val = "%" +request.getParameter("PE_ID").toString()+ "%";
        DSqlKit.append(" and (PE_ID like ?) ", val);
    }
    if(!ObjectUtils.isEmpty(request.getParameter("PE_VISIT_ID")))
    {
        DSqlKit.append(" and (PE_VISIT_ID = ?) ", request.getParameter("PE_VISIT_ID").toString());
    }
    if(!ObjectUtils.isEmpty(request.getParameter("ID_NO")))
    {
        String val = "%" +request.getParameter("ID_NO").toString()+ "%";
        DSqlKit.append(" and (ID_NO like ?) ", val);
    }
    if(!ObjectUtils.isEmpty(request.getParameter("BH")))
    {
        String val = "%" +request.getParameter("BH").toString()+ "%";
        DSqlKit.append(" and (BH like ?) ", val);
    }
    if(!ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
    {
        String val = "%" +request.getParameter("UPLOAD").toString()+ "%";
        DSqlKit.append(" and (UPLOAD like ?) ", val);
    }

    if(ObjectUtils.isEmpty(request.getParameter("templateId")) && ObjectUtils.isEmpty(request.getParameter("saveFile")) && ObjectUtils.isEmpty(request.getParameter("createDateRange[0]"))
            && ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && ObjectUtils.isEmpty(request.getParameter("PE_ID")) && ObjectUtils.isEmpty(request.getParameter("ID_NO")) && ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
    {
        DSqlKit.append(" and ((SAVE_FILE = '0') or (REPORT_DATE>  CURDATE()-1))");
    }

    DSqlKit.append(" order by  REPORT_DATE desc ");
    Page<Record> page =  Db.paginate(pageIndex, pageSize,select,DSqlKit.getSql(),DSqlKit.getParamList());
    String[] tmp={"SIXTEEN","EIGHTEEN","THIRTY_ONE","THIRTY_THREE","THIRTY_FIVE","FORTY_FIVE","FIFTY_TWO"
            ,"FIFTY_EIGHT","FURTHER_EXAMINATION","CERVICAL_CYTOLOGY","VIA","COLPOSCOPY"};
    for(Record data:page.getList())
    {
        for(String tmpStr:tmp)
        {
            if(data.get(tmpStr)!=null)
            {
                if(data.getStr(tmpStr).equals("1"))
                {
                    data.set(tmpStr,true);
                }
                else
                {
                    data.set(tmpStr,false);
                }
            }
            else
            {
                data.set(tmpStr,false);
            }
        }
    }

    return getDataTable(page);
}

    @ResponseBody
    @Log(title = "宫颈癌hpv审核保存", businessType = BusinessType.UPDATE)
    @PostMapping("/hpvsave")
    public AjaxResult hpvsave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Boolean flag=hpvsaveData(data,loginUser.getUsername());
        if(flag)
        {
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error();
        }

    }


    @ResponseBody
    @Log(title = "宫颈癌hpv审核批量保存", businessType = BusinessType.UPDATE)
    @PostMapping("/hpvbatchSave")
    public AjaxResult hpvbatchSave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String Msg="";
        List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
        for(Map<String,Object> tmp : list)
        {
            if(tmp.get("upload")==null)
            {
                Boolean flag=hpvsaveData(tmp,loginUser.getUsername());
                if(!flag)
                {
                    Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                }
            }
            else
            {
                if(!tmp.get("upload").toString().equals("1"))
                {
                    Boolean flag=hpvsaveData(tmp,loginUser.getUsername());
                    if(!flag)
                    {
                        Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                    }
                }
            }
        }
        return AjaxResult.success(Msg);
    }


    public boolean hpvsaveData(Map<String,Object> data,String userName)
    {
        Record saveGjgbsData=new Record();
        saveGjgbsData.set("PE_ID",data.get("peId"));
        saveGjgbsData.set("PE_VISIT_ID",data.get("peVisitId"));
        saveGjgbsData.set("ID_NO",data.get("idNo"));
//        if(data.get("lastMenstrualPeriod")!=null)
//        {
//            saveGjgbsData.set("LAST_MENSTRUAL_PERIOD",data.get("lastMenstrualPeriod").toString().substring(0,10));
//        }
        saveGjgbsData.set("HPV_CHECK",data.get("hpvCheck"));
        saveGjgbsData.set("HPV_TYPING",data.get("hpvTyping"));

        saveGjgbsData.set("SAVE_FILE","1");
        saveGjgbsData.set("dept_result",data.get("deptResult"));
        saveGjgbsData.set("result",data.get("result"));
        saveGjgbsData.set("REPORT_DATE",new Date());
        saveGjgbsData.set("REPORT_PERSON",userName);
        saveGjgbsData.set("SAVE_FILE","1");




        Map<String,String> tmpMap=new HashMap<>();


        tmpMap.put("sixteen","SIXTEEN");
        tmpMap.put("eighteen","EIGHTEEN");
        tmpMap.put("thirtyOne","THIRTY_ONE");
        tmpMap.put("thirtyThree","THIRTY_THREE");
        tmpMap.put("thirtyFive","THIRTY_FIVE");
        tmpMap.put("fortyFive","FORTY_FIVE");
        tmpMap.put("fiftyTwo","FIFTY_TWO");
        tmpMap.put("fiftyEight","FIFTY_EIGHT");
        tmpMap.put("furtherExamination","FURTHER_EXAMINATION");
        tmpMap.put("cervicalCytology","CERVICAL_CYTOLOGY");
        tmpMap.put("colposcopy","COLPOSCOPY");


        for(String key:tmpMap.keySet())
        {
            if(data.get(key)==null)
            {
                saveGjgbsData.set(tmpMap.get(key),"0");
            }
            else
            {
                if((Boolean) data.get(key))
                {
                    saveGjgbsData.set(tmpMap.get(key),"1");
                }
                else
                {
                    saveGjgbsData.set(tmpMap.get(key),"0");
                }
            }
        }


        System.out.println(saveGjgbsData);


        Boolean flag=Db.tx(() -> {
            Db.update("gjghpvjc","PE_ID,PE_VISIT_ID",saveGjgbsData);
//            Db.update("gjgfkjc","PE_ID,PE_VISIT_ID",saveGjgfkjcData);
            return true;
        });
        return flag;
    }
//*************************************************xbx**********************************************************************
@GetMapping("/xbxlist")
public TableDataInfo xbxlist(HttpServletRequest request)
{

    PageDomain pageDomain = TableSupport.buildPageRequest();
    Integer pageIndex = pageDomain.getPageNum();
    Integer pageSize = pageDomain.getPageSize();

    String select="select     PE_ID ,  PE_VISIT_ID ,  ID_NO ,  INTREAPITHELIAL_LESIONS ,  ASC_US ,  ASC_H,  LSIL,  HSIL,\n" +
            "  SCC,  AGC ,  ATYPICAL_ENDOCERVICAL,  ENDOCERVICAL_CARCINOMA,  ADENOCARCINOMA ,  COLPOSCOPY,\n" +
            "  REPORT_DATE,  REPORT_PERSON,  P_ID,  AUTO_VALUE,  PE_DATE ,  DOCTOR_NAME,  SAVE_FILE,  UPLOAD ,  OTHER ,  UPLOADRESULT,  SAMPLING_METHODS,dept_result,result,creat_time ,check_type";
    DSqlKit.init(" from gjgxbxjc  where 1=1 ");

    //上传状态
    if(!ObjectUtils.isEmpty(request.getParameter("templateId")))
    {
        DSqlKit.append(" and (UPLOAD = ?) ", request.getParameter("templateId").toString());
    }
    if(!ObjectUtils.isEmpty(request.getParameter("saveFile")))
    {
        DSqlKit.append(" and (SAVE_FILE = ?) ", request.getParameter("saveFile").toString());
    }




    if(!ObjectUtils.isEmpty(request.getParameter("createDateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("createDateRange[1]")))
    {

        DSqlKit.append(" and (creat_time BETWEEN ? and ? ) ", request.getParameter("createDateRange[0]"),request.getParameter("createDateRange[1]"));
    }
    if(!ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("dateRange[1]")))
    {

        DSqlKit.append(" and (REPORT_DATE BETWEEN ? and ? ) ", request.getParameter("dateRange[0]"),request.getParameter("dateRange[1]"));
    }

    if(!ObjectUtils.isEmpty(request.getParameter("PE_ID")))
    {
        String val = "%" +request.getParameter("PE_ID").toString()+ "%";
        DSqlKit.append(" and (PE_ID like ?) ", val);
    }
    if(!ObjectUtils.isEmpty(request.getParameter("PE_VISIT_ID")))
    {
        DSqlKit.append(" and (PE_VISIT_ID = ?) ", request.getParameter("PE_VISIT_ID").toString());
    }
    if(!ObjectUtils.isEmpty(request.getParameter("ID_NO")))
    {
        String val = "%" +request.getParameter("ID_NO").toString()+ "%";
        DSqlKit.append(" and (ID_NO like ?) ", val);
    }
    if(!ObjectUtils.isEmpty(request.getParameter("BH")))
    {
        String val = "%" +request.getParameter("BH").toString()+ "%";
        DSqlKit.append(" and (BH like ?) ", val);
    }
    if(!ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
    {
        String val = "%" +request.getParameter("UPLOAD").toString()+ "%";
        DSqlKit.append(" and (UPLOAD like ?) ", val);
    }

    if(ObjectUtils.isEmpty(request.getParameter("templateId")) && ObjectUtils.isEmpty(request.getParameter("saveFile")) && ObjectUtils.isEmpty(request.getParameter("createDateRange[0]"))
            && ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && ObjectUtils.isEmpty(request.getParameter("PE_ID")) && ObjectUtils.isEmpty(request.getParameter("ID_NO")) && ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
    {
        DSqlKit.append(" and ((SAVE_FILE = '0') or (REPORT_DATE>  CURDATE()-1))");
    }

    DSqlKit.append(" order by  REPORT_DATE desc ");
    Page<Record> page =  Db.paginate(pageIndex, pageSize,select,DSqlKit.getSql(),DSqlKit.getParamList());
    String[] tmp={"CONDYLOMA_ACUMINATUM","TRICHOMONAS_VAGINITIS","VULVOVAGINAL_CANDIDIASIS",
            "BACTERIAL_VAGINOSIS","MUCOPURULENT_CERVICITIS","CERVICAL_POLYP","FIBROID","OTHER"};
    for(Record data:page.getList())
    {
        for(String tmpStr:tmp)
        {
            if(data.get(tmpStr)!=null)
            {
                if(data.getStr(tmpStr).equals("1"))
                {
                    data.set(tmpStr,true);
                }
                else
                {
                    data.set(tmpStr,false);
                }
            }
            else
            {
                data.set(tmpStr,false);
            }
        }
    }
    return getDataTable(page);
}

    @ResponseBody
    @Log(title = "宫颈癌细胞学审核保存", businessType = BusinessType.UPDATE)
    @PostMapping("/xbxsave")
    public AjaxResult xbxsave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Boolean flag=xbxsaveData(data,loginUser.getUsername());
        if(flag)
        {
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error();
        }

    }


    @ResponseBody
    @Log(title = "宫颈癌细胞学批量保存", businessType = BusinessType.UPDATE)
    @PostMapping("/xbxbatchSave")
    public AjaxResult xbxbatchSave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String Msg="";
        List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
        for(Map<String,Object> tmp : list)
        {
            if(tmp.get("upload")==null)
            {
                Boolean flag=xbxsaveData(tmp,loginUser.getUsername());
                if(!flag)
                {
                    Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                }
            }
            else
            {
                if(!tmp.get("upload").toString().equals("1"))
                {
                    Boolean flag=xbxsaveData(tmp,loginUser.getUsername());
                    if(!flag)
                    {
                        Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                    }
                }
            }
        }
        return AjaxResult.success(Msg);
    }


    public boolean xbxsaveData(Map<String,Object> data,String userName)
    {
        System.out.println(data);
        Record saveGjgbsData=new Record();
        saveGjgbsData.set("PE_ID",data.get("peId"));
        saveGjgbsData.set("PE_VISIT_ID",data.get("peVisitId"));
        saveGjgbsData.set("ID_NO",data.get("idNo"));
//        if(data.get("lastMenstrualPeriod")!=null)
//        {
//            saveGjgbsData.set("LAST_MENSTRUAL_PERIOD",data.get("lastMenstrualPeriod").toString().substring(0,10));
//        }
//        saveGjgbsData.set("HPV_CHECK",data.get("hpvCheck"));
//        saveGjgbsData.set("HPV_TYPING",data.get("hpvTyping"));

        saveGjgbsData.set("SAVE_FILE","1");
        saveGjgbsData.set("dept_result",data.get("deptResult"));
        saveGjgbsData.set("result",data.get("result"));
        saveGjgbsData.set("COLPOSCOPY",data.get("colposcopy"));
        saveGjgbsData.set("check_type",data.get("checkType"));

        String  checkType="0";
        if(!ObjectUtils.isEmpty(data.get("checkType")))
        {
              checkType=data.get("checkType").toString();
        }


//****************************对应前端名称
//            <span v-else-if="scope.row.checkType=='0'">无</span>
//        <span v-else-if="scope.row.checkType=='1'">未见上皮内病变细胞和恶性细胞</span>
//        <span v-else-if="scope.row.checkType=='2'">未明确意义的不典型鳞状细胞（asc_us）</span>
//        <span v-else-if="scope.row.checkType=='3'">不典型鳞状上皮细胞不除外高度鳞状上皮内病变（asc-h）</span>
//        <span v-else-if="scope.row.checkType=='4'">低度鳞状上皮内病变（lsil）</span>
//        <span v-else-if="scope.row.checkType=='5'">高度鳞状上皮内病变（hsil）</span>
//        <span v-else-if="scope.row.checkType=='6'">鳞状细胞癌（scc）</span>
//        <span v-else-if="scope.row.checkType=='7'">不典型腺上皮细胞（agc）</span>
//        <span v-else-if="scope.row.checkType=='8'">不典型宫颈管腺细胞倾向瘤变</span>
//        <span v-else-if="scope.row.checkType=='9'">宫颈管原位癌</span>
//        <span v-else-if="scope.row.checkType=='10'">腺癌</span>
        switch(checkType){
            case "1" :
             saveGjgbsData.set("INTREAPITHELIAL_LESIONS","1");
                saveGjgbsData.set("ASC_US","0");
                saveGjgbsData.set("ASC_H","0");
                saveGjgbsData.set("LSIL","0");
                saveGjgbsData.set("HSIL","0");
                saveGjgbsData.set("SCC","0");
                saveGjgbsData.set("AGC","0");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","0");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","0");
                saveGjgbsData.set("ADENOCARCINOMA","0");
            break;
            case "2" :
                saveGjgbsData.set("INTREAPITHELIAL_LESIONS","0");
                saveGjgbsData.set("ASC_US","1");
                saveGjgbsData.set("ASC_H","0");
                saveGjgbsData.set("LSIL","0");
                saveGjgbsData.set("HSIL","0");
                saveGjgbsData.set("SCC","0");
                saveGjgbsData.set("AGC","0");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","0");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","0");
                saveGjgbsData.set("ADENOCARCINOMA","0");
                break;
            case "3" :
                saveGjgbsData.set("INTREAPITHELIAL_LESIONS","0");
                saveGjgbsData.set("ASC_US","0");
                saveGjgbsData.set("ASC_H","1");
                saveGjgbsData.set("LSIL","0");
                saveGjgbsData.set("HSIL","0");
                saveGjgbsData.set("SCC","0");
                saveGjgbsData.set("AGC","0");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","0");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","0");
                saveGjgbsData.set("ADENOCARCINOMA","0");
                break;
            case "4" :
                saveGjgbsData.set("INTREAPITHELIAL_LESIONS","0");
                saveGjgbsData.set("ASC_US","0");
                saveGjgbsData.set("ASC_H","0");
                saveGjgbsData.set("LSIL","1");
                saveGjgbsData.set("HSIL","0");
                saveGjgbsData.set("SCC","0");
                saveGjgbsData.set("AGC","0");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","0");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","0");
                saveGjgbsData.set("ADENOCARCINOMA","0");
                break;
            case "5" :
                saveGjgbsData.set("INTREAPITHELIAL_LESIONS","0");
                saveGjgbsData.set("ASC_US","0");
                saveGjgbsData.set("ASC_H","0");
                saveGjgbsData.set("LSIL","0");
                saveGjgbsData.set("HSIL","1");
                saveGjgbsData.set("SCC","0");
                saveGjgbsData.set("AGC","0");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","0");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","0");
                saveGjgbsData.set("ADENOCARCINOMA","0");
                break;
            case "6" :
                saveGjgbsData.set("INTREAPITHELIAL_LESIONS","0");
                saveGjgbsData.set("ASC_US","0");
                saveGjgbsData.set("ASC_H","0");
                saveGjgbsData.set("LSIL","0");
                saveGjgbsData.set("HSIL","0");
                saveGjgbsData.set("SCC","1");
                saveGjgbsData.set("AGC","0");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","0");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","0");
                saveGjgbsData.set("ADENOCARCINOMA","0");
                break;
            case "7" :
                saveGjgbsData.set("INTREAPITHELIAL_LESIONS","10");
                saveGjgbsData.set("ASC_US","0");
                saveGjgbsData.set("ASC_H","0");
                saveGjgbsData.set("LSIL","0");
                saveGjgbsData.set("HSIL","0");
                saveGjgbsData.set("SCC","0");
                saveGjgbsData.set("AGC","1");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","0");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","0");
                saveGjgbsData.set("ADENOCARCINOMA","0");
                break;
            case "8" :
                saveGjgbsData.set("INTREAPITHELIAL_LESIONS","0");
                saveGjgbsData.set("ASC_US","0");
                saveGjgbsData.set("ASC_H","0");
                saveGjgbsData.set("LSIL","0");
                saveGjgbsData.set("HSIL","0");
                saveGjgbsData.set("SCC","0");
                saveGjgbsData.set("AGC","0");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","1");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","0");
                saveGjgbsData.set("ADENOCARCINOMA","0");
                break;
            case "9" :
                saveGjgbsData.set("INTREAPITHELIAL_LESIONS","0");
                saveGjgbsData.set("ASC_US","0");
                saveGjgbsData.set("ASC_H","0");
                saveGjgbsData.set("LSIL","0");
                saveGjgbsData.set("HSIL","0");
                saveGjgbsData.set("SCC","0");
                saveGjgbsData.set("AGC","0");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","0");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","1");
                saveGjgbsData.set("ADENOCARCINOMA","0");
                break;
            case "10" :
                saveGjgbsData.set("INTREAPITHELIAL_LESIONS","0");
                saveGjgbsData.set("ASC_US","0");
                saveGjgbsData.set("ASC_H","0");
                saveGjgbsData.set("LSIL","0");
                saveGjgbsData.set("HSIL","0");
                saveGjgbsData.set("SCC","0");
                saveGjgbsData.set("AGC","0");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","0");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","0");
                saveGjgbsData.set("ADENOCARCINOMA","1");
                break;
            default:
                saveGjgbsData.set("INTREAPITHELIAL_LESIONS","0");
                saveGjgbsData.set("ASC_US","0");
                saveGjgbsData.set("ASC_H","0");
                saveGjgbsData.set("LSIL","0");
                saveGjgbsData.set("HSIL","0");
                saveGjgbsData.set("SCC","0");
                saveGjgbsData.set("AGC","0");
                saveGjgbsData.set("ATYPICAL_ENDOCERVICAL","0");
                saveGjgbsData.set("ENDOCERVICAL_CARCINOMA","0");
                saveGjgbsData.set("ADENOCARCINOMA","0");
                break;
        }




        saveGjgbsData.set("REPORT_DATE",new Date());
        saveGjgbsData.set("REPORT_PERSON",userName);
        saveGjgbsData.set("SAVE_FILE","1");

        System.out.println(saveGjgbsData);
        Boolean flag=Db.tx(() -> {
            Db.update("gjgxbxjc","PE_ID,PE_VISIT_ID",saveGjgbsData);

            return true;
        });
        return flag;
    }


// *************************************************ydj**********************************************************************
@GetMapping("/ydjlist")
public TableDataInfo ydjlist(HttpServletRequest request)
{

    PageDomain pageDomain = TableSupport.buildPageRequest();
    Integer pageIndex = pageDomain.getPageNum();
    Integer pageSize = pageDomain.getPageSize();

    String select="select    PE_ID ,  PE_VISIT_ID ,  COLPOSCOPY,  COLPOSCOPY_EVALUATION,  HISTOPATHOLOGICAL_EXAMINATION ,  PRELIMINARY_DIAGNOSIS,\n" +
            "  ID_NO,  P_ID,  AUTO_VALUE,  PE_DATE ,  DOCTOR_NAME ,  SAVE_FILE ,  UPLOAD ,  OTHER ,  REPORT_PERSON,  REPORT_DATE  ,  UPLOADRESULT ,dept_result,result,creat_time ";
    DSqlKit.init(" from gjgydjjc  where 1=1 ");
//上传状态
    if(!ObjectUtils.isEmpty(request.getParameter("templateId")))
    {
        DSqlKit.append(" and (UPLOAD = ?) ", request.getParameter("templateId").toString());
    }
    if(!ObjectUtils.isEmpty(request.getParameter("saveFile")))
    {
        DSqlKit.append(" and (SAVE_FILE = ?) ", request.getParameter("saveFile").toString());
    }


    if(!ObjectUtils.isEmpty(request.getParameter("createDateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("createDateRange[1]")))
    {

        DSqlKit.append(" and (creat_time BETWEEN ? and ? ) ", request.getParameter("createDateRange[0]"),request.getParameter("createDateRange[1]"));
    }
    if(!ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && !ObjectUtils.isEmpty(request.getParameter("dateRange[1]")))
    {

        DSqlKit.append(" and (REPORT_DATE BETWEEN ? and ? ) ", request.getParameter("dateRange[0]"),request.getParameter("dateRange[1]"));
    }

    if(!ObjectUtils.isEmpty(request.getParameter("PE_ID")))
    {
        String val = "%" +request.getParameter("PE_ID").toString()+ "%";
        DSqlKit.append(" and (PE_ID like ?) ", val);
    }
    if(!ObjectUtils.isEmpty(request.getParameter("PE_VISIT_ID")))
    {
        DSqlKit.append(" and (PE_VISIT_ID = ?) ", request.getParameter("PE_VISIT_ID").toString());
    }
    if(!ObjectUtils.isEmpty(request.getParameter("ID_NO")))
    {
        String val = "%" +request.getParameter("ID_NO").toString()+ "%";
        DSqlKit.append(" and (ID_NO like ?) ", val);
    }
    if(!ObjectUtils.isEmpty(request.getParameter("BH")))
    {
        String val = "%" +request.getParameter("BH").toString()+ "%";
        DSqlKit.append(" and (BH like ?) ", val);
    }
    if(!ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
    {
        String val = "%" +request.getParameter("UPLOAD").toString()+ "%";
        DSqlKit.append(" and (UPLOAD like ?) ", val);
    }

    if(ObjectUtils.isEmpty(request.getParameter("templateId")) && ObjectUtils.isEmpty(request.getParameter("saveFile")) && ObjectUtils.isEmpty(request.getParameter("createDateRange[0]"))
            && ObjectUtils.isEmpty(request.getParameter("dateRange[0]")) && ObjectUtils.isEmpty(request.getParameter("PE_ID")) && ObjectUtils.isEmpty(request.getParameter("ID_NO")) && ObjectUtils.isEmpty(request.getParameter("UPLOAD")))
    {
        DSqlKit.append(" and ((SAVE_FILE = '0') or (REPORT_DATE>  CURDATE()-1))");
    }

    DSqlKit.append(" order by  REPORT_DATE desc ");
    Page<Record> page =  Db.paginate(pageIndex, pageSize,select,DSqlKit.getSql(),DSqlKit.getParamList());
    String[] tmp={"CONDYLOMA_ACUMINATUM","TRICHOMONAS_VAGINITIS","VULVOVAGINAL_CANDIDIASIS",
            "BACTERIAL_VAGINOSIS","MUCOPURULENT_CERVICITIS","CERVICAL_POLYP","FIBROID","OTHER"};
    for(Record data:page.getList())
    {
        for(String tmpStr:tmp)
        {
            if(data.get(tmpStr)!=null)
            {
                if(data.getStr(tmpStr).equals("1"))
                {
                    data.set(tmpStr,true);
                }
                else
                {
                    data.set(tmpStr,false);
                }
            }
            else
            {
                data.set(tmpStr,false);
            }
        }
    }
    return getDataTable(page);
}

    @ResponseBody
    @Log(title = "宫颈癌病史审核保存", businessType = BusinessType.UPDATE)
    @PostMapping("/ydjsave")
    public AjaxResult ydjsave(@RequestBody(required = true) Map<String,Object> data)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Boolean flag=ydjsaveData(data,loginUser.getUsername());
        if(flag)
        {
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error();
        }

    }


    @ResponseBody
    @Log(title = "宫颈癌病史审核批量保存", businessType = BusinessType.UPDATE)
    @PostMapping("/ydjbatchSave")
    public AjaxResult ydjbatchSave(@RequestBody(required = true) Map<String,Object> data)
    {
        System.out.println(data);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String Msg="";
        List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
        for(Map<String,Object> tmp : list)
        {
            if(tmp.get("upload")==null)
            {
                Boolean flag=ydjsaveData(tmp,loginUser.getUsername());
                if(!flag)
                {
                    Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                }
            }
            else
            {
                if(!tmp.get("upload").toString().equals("1"))
                {
                    Boolean flag=ydjsaveData(tmp,loginUser.getUsername());
                    if(!flag)
                    {
                        Msg+="["+data.get("peId")+"]-["+tmp.get("peVisitId")+"]数据保存失败！\r\n";
                    }
                }
            }
        }
        return AjaxResult.success(Msg);
    }


    public boolean ydjsaveData(Map<String,Object> data,String userName)
    {
        Record saveGjgbsData=new Record();
        saveGjgbsData.set("PE_ID",data.get("peId"));
        saveGjgbsData.set("PE_VISIT_ID",data.get("peVisitId"));
        saveGjgbsData.set("ID_NO",data.get("idNo"));

        saveGjgbsData.set("COLPOSCOPY",data.get("colposcopy"));
        saveGjgbsData.set("COLPOSCOPY_EVALUATION",data.get("colposcopyEvaluation"));
        saveGjgbsData.set("HISTOPATHOLOGICAL_EXAMINATION",data.get("preliminaryDiagnosis"));
        saveGjgbsData.set("PRELIMINARY_DIAGNOSIS",data.get("histopathologicalExamination"));


        saveGjgbsData.set("REPORT_DATE",new Date());
        saveGjgbsData.set("REPORT_PERSON",userName);
        saveGjgbsData.set("SAVE_FILE","1");
//        saveGjgbsData.set("dept_result",data.get("deptResult"));
//        saveGjgbsData.set("result",data.get("result"));


        Boolean flag=Db.tx(() -> {

            Db.update("gjgydjjc","PE_ID,PE_VISIT_ID",saveGjgbsData);
            return true;
        });
        return flag;
    }

//    @ResponseBody
    @Log(title = "上传前校验各表要上传完成", businessType = BusinessType.UPDATE)
    @GetMapping ("/gjgtest")
    public AjaxResult gjgtest(HttpServletRequest request)
    {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
        String Msg="";

        Record bsData=Db.findFirst("SELECT * FROM gjgbs where save_file<>'1'");
        if(bsData!=null) {
            Msg+="宫颈癌病史有未审核数据";
            return AjaxResult.error(Msg);
        }
//        Record bsDatasc=Db.findFirst("SELECT * FROM gjgbs where upload='2'");
//        if(bsDatasc!=null) {
//            Msg+="宫颈癌病史有未完成上传数据，可能在上传，请等等";
//            return AjaxResult.error(Msg);
//        }
        Record hpvData=Db.findFirst("SELECT * FROM gjghpvjc where save_file<>'1'");
        if(hpvData!=null) {
            Msg+="宫颈癌HPV有未审核数据";
            return AjaxResult.error(Msg);
        }
        Record xbxData=Db.findFirst("SELECT * FROM gjgxbxjc where save_file<>'1'");
        if(xbxData!=null) {
            Msg+="宫颈癌细胞学有未审核数据";
            return AjaxResult.error(Msg);
        }
        Record ydjData=Db.findFirst("SELECT * FROM gjgydjjc where save_file<>'1'");
        if(ydjData!=null) {
            Msg+="宫颈癌阴道镜有未审核数据";
            return AjaxResult.error(Msg);
        }

        return AjaxResult.success(Msg);
    }


}
